package ciyuan.Item.SkillBook;

import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;

public class ThunderFalls extends SkillBookType {

    public ThunderFalls() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 25 * 1000);
        if (playerEntity.level.isClientSide) {
            return;
        }
        BlockPos blockPos = SkillUtils.getPlayerSee(playerEntity.level, playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY, 10).getBlockPos();
        for (LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                blockPos.getX() + 3, blockPos.getY() + 3, blockPos.getZ() + 3, blockPos.getX() - 3, blockPos.getY() - 3, blockPos.getZ() - 3
        ))) {
            if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) && blockPos.distSqr(livingentity.getX(),livingentity.getY(),livingentity.getZ(),false) < 8.0D) {
                livingentity.addEffect(new EffectInstance(Effects.thunder_falls,100,0));
                ServerWorld serverWorld = (ServerWorld) playerEntity.level;
                LightningBoltEntity lightningboltentity = EntityType.LIGHTNING_BOLT.create(serverWorld);
                lightningboltentity.moveTo(Vector3d.atBottomCenterOf(livingentity.blockPosition()));
                lightningboltentity.setVisualOnly(true);
                SkillUtils.addEntity(lightningboltentity,serverWorld);
                livingentity.hurt(DamageSource.playerAttack(playerEntity),10);
            }
        }
        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "ThunderFalls";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.36");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.36");
    }
}
